package com.changgou.canal.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: 陈安
 * @Date: 2020/12/09 9:14
 * @Description: $todo$
 * @Version 1.0
 */
@Configuration
public class RabbitMQConfig {

    //定义商品交换机名称
    public static final String GOODS_UP_EXCHAGE="goods_up_exchange";

    //定义的是下架商品的交换机
    public static final String GOODS_DOWN_EXCHANGE="goods_down_exchange";

    //定义队列名称
    public static final String AD_UPDATE_QUEUE = "ad_update_queue";
    //这个队列是做上架商品的
    public static  final String SEARCH_ADD_QUEUE="search_add_queue";

    //定义的是下架商品的队列
    public static final String SEARCH_DEL_QUEUE="goods_down_exchange";

    //声明队列
    @Bean
    public Queue AD_UPDATE_QUEUE(){
        return new Queue(AD_UPDATE_QUEUE);
    }
    //声明商品队列
    @Bean(SEARCH_ADD_QUEUE)
    public Queue SEARCH_ADD_QUEUE(){
        return new Queue(SEARCH_ADD_QUEUE);
    }

    //声明的是下架商品的交换机
    @Bean(SEARCH_DEL_QUEUE)
    public Queue SEARCH_DEL_QUEUE(){
        return new Queue(SEARCH_DEL_QUEUE);
    }

    //声明商品交换机
    @Bean(GOODS_UP_EXCHAGE)
    public Exchange GOODS_UP_EXCHAGE(){
        return ExchangeBuilder.fanoutExchange(GOODS_UP_EXCHAGE).durable(true).build();
    }

    //声明下架商品的交换机
    @Bean(GOODS_DOWN_EXCHANGE)
    public Exchange GOODS_DOWN_EXCHANGE(){
        return ExchangeBuilder.fanoutExchange(GOODS_DOWN_EXCHANGE).durable(true).build();
    }

    //队列与交换机的绑定,通过Qualifier
    @Bean
    public Binding GOODS_UP_EXCHAGE_BINDING(@Qualifier(SEARCH_ADD_QUEUE)Queue queue,
                                            @Qualifier(GOODS_UP_EXCHAGE)Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with("").noargs();

    }

    @Bean
    public Binding GOODS_DOWN_EXCHANGE_BINDING(@Qualifier(SEARCH_DEL_QUEUE)Queue queue,
                                                    @Qualifier(GOODS_DOWN_EXCHANGE)Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
}
